#include "gamewidget.h"
#include<QPainter>
#include<QPen>
#include<QBrush>
#include<QRandomGenerator>
#include<QDebug>
#include<QVBoxLayout>
#include<QPixmap>

GameWidget::GameWidget(QWidget* parent):QWidget(parent) {
    setFocusPolicy(Qt::StrongFocus);

    gridSize=20;
    catSize=2;
    mouseSize=1;
    catSpeed=100;
    mouseSpeed=100;
    mousefeel=5;
    timeLeft=100;
    paused=false;
    firstcatched=false;

    gameTimer=new QTimer(this);
    mouseTimer=new QTimer(this);
    countdownTimer=new QTimer(this);

  //  setupScene();

    connect(gameTimer,&QTimer::timeout,this,&GameWidget::updateGame);
    connect(mouseTimer,&QTimer::timeout,this,&GameWidget::moveMouse);
    connect(countdownTimer,&QTimer::timeout,[this](){
        timeLeft--;
        timeLabel->setText(QString("还有：%1秒").arg(timeLeft));
        if(timeLeft<=0){
            gameTimer->stop();
            mouseTimer->stop();
            countdownTimer->stop();
            emit gameOver(false);
        }
    });

    timeLabel=new QLabel(QString("还有：%1秒").arg(timeLeft),this);
    timeLabel->setAlignment(Qt::AlignRight);
    timeLabel->setStyleSheet("font-size:16px;color:black;");

    QVBoxLayout* layout=new QVBoxLayout(this);
    layout->addWidget(timeLabel);
    layout->addStretch();
    setLayout(layout);
}


void GameWidget::startGame(){
    cat={5,5};
    mouse={15,15};
    timeLeft=100;
    timeLabel->setText(QString("还有：%1秒").arg(timeLeft));

    gameTimer->start(16);
    mouseTimer->start(mouseSpeed);
    countdownTimer->start(1000);
    paused=false;
    update();
}

void GameWidget::resumeGame(){
    gameTimer->start();
    mouseTimer->start();
    countdownTimer->start();
    paused=false;
}


void GameWidget::paintEvent(QPaintEvent* event){
    Q_UNUSED(event);

    QPainter painter(this);
    painter.setRenderHint(QPainter::Antialiasing);

    if(!firstcatched){
        painter.setPen(QPen(Qt::black));
        for(int x=0;x<=width();x+=gridSize){
            painter.drawLine(x,0,x,x);
        }

        painter.setBrush(QBrush(QColor(70,150,100)));
        for(int x=0;x<=width();x+=gridSize){
            painter.drawRect(x,0,x,height());
        }
        for(int y=0;y<=height();y+=gridSize){
            painter.drawRect(0,y,width(),y);
        }


    QPixmap bgPixmap(":/images/pictures/thebackground.webp");
    painter.drawPixmap(0,0,width(),height(),bgPixmap);

    painter.setBrush(QBrush(QColor(255,165,0)));//猫底色
    painter.drawRect(cat.x*gridSize,cat.y*gridSize,catSize*gridSize,catSize*gridSize);

    painter.setBrush(QBrush(Qt::white));//猫眼白
    painter.drawEllipse(cat.x*gridSize+5,cat.y*gridSize+5,10,10);
    painter.drawEllipse(cat.x*gridSize+25,cat.y*gridSize+5,10,10);

    painter.setBrush(QBrush(Qt::black));//猫眼黑
    painter.drawEllipse(cat.x*gridSize+8,cat.y*gridSize+8,4,4);
    painter.drawEllipse(cat.x*gridSize+28,cat.y*gridSize+8,4,4);

    painter.setBrush(QBrush(QColor(169,169,169)));//鼠底色
    painter.drawRect(mouse.x*gridSize,mouse.y*gridSize,mouseSize*gridSize,mouseSize*gridSize);

    painter.setBrush(QBrush(Qt::black));//鼠眼黑
    painter.drawEllipse(mouse.x*gridSize+5,mouse.y*gridSize+5,4,4);
    painter.drawEllipse(mouse.x*gridSize+11,mouse.y*gridSize+5,4,4);

    if(paused){
        painter.setBrush(QBrush(QColor(80,170,0,180)));
        painter.drawRect(0,0,width(),height());
        painter.setPen(QPen(Qt::white));
        painter.setFont(QFont("Arial",24,QFont::Bold));
        painter.drawText(rect(),Qt::AlignCenter,"The_World!\n按ESC继续");
    }
    }else{

        painter.setPen(QPen(Qt::black));
        for(int x=0;x<=width();x+=gridSize){
            painter.drawLine(x,0,height(),x);
        }

        painter.setBrush(QBrush(QColor(200,70,100)));
        for(int x=0;x<=width();x+=gridSize){
            painter.drawRect(x,0,x,height());
        }
        for(int y=0;y<=height();y+=gridSize){
            painter.drawRect(0,y,width(),y);
        }

        painter.setBrush(QBrush(QColor(255,165,0)));//猫底色
        painter.drawRect(cat.x*gridSize,cat.y*gridSize,catSize*gridSize,catSize*gridSize);

        painter.setBrush(QBrush(Qt::white));//猫眼白
        painter.drawEllipse(cat.x*gridSize+5,cat.y*gridSize+5,10,10);
        painter.drawEllipse(cat.x*gridSize+25,cat.y*gridSize+5,10,10);

        painter.setBrush(QBrush(Qt::black));//猫眼黑
        painter.drawEllipse(cat.x*gridSize+8,cat.y*gridSize+8,4,4);
        painter.drawEllipse(cat.x*gridSize+28,cat.y*gridSize+8,4,4);

        painter.setBrush(QBrush(QColor(170,70,169)));//鼠底色
        painter.drawRect(mouse.x*gridSize,mouse.y*gridSize,mouseSize*gridSize,mouseSize*gridSize);

        painter.setBrush(QBrush(Qt::black));//鼠眼黑
        painter.drawEllipse(mouse.x*gridSize+5,mouse.y*gridSize+5,4,4);
        painter.drawEllipse(mouse.x*gridSize+11,mouse.y*gridSize+5,4,4);


        if(paused){
            painter.setBrush(QBrush(QColor(200,60,0,180)));
            painter.drawRect(0,0,width(),height());
            painter.setPen(QPen(Qt::white));
            painter.setFont(QFont("Arial",24,QFont::Bold));
            painter.drawText(rect(),Qt::AlignCenter,"The_World!\n按ESC继续");
        }
    }

}

void GameWidget::keyPressEvent(QKeyEvent* event){
    if(paused&&event->key()!=Qt::Key_Escape){
        return;
    }

    switch(event->key()){
    case Qt::Key_Left:
        if(cat.x>0)cat.x--;
        break;
    case Qt::Key_Right:
        if(cat.x<(width()/gridSize)-catSize)cat.x++;
        break;
    case Qt::Key_Up:
        if(cat.y>0)cat.y--;
        break;
    case Qt::Key_Down:
        if(cat.y<(height()/gridSize)-catSize)cat.y++;
        break;
    case Qt::Key_Escape:
        paused=!paused;
        if(paused){
            gameTimer->stop();
            mouseTimer->stop();
            countdownTimer->stop();
            emit gamePaused();
        }else{
            gameTimer->start();
            mouseTimer->start();
            countdownTimer->start();
        }
        break;
    default:
        QWidget::keyPressEvent(event);
    }
    update();
}

void GameWidget::updateGame(){
    checkCollision();
    update();
}

void GameWidget::moveMouse(){
    if(isCatNearby(mouse.x,mouse.y)){
        int dx=0,dy=0;

        if(mouse.x<cat.x)dx=-3;
        else if(mouse.x>cat.x+catSize-1)dx=3;

        if(mouse.y<cat.y)dy=-3;
        else if(mouse.y>cat.y+catSize-1)dy=3;

        if(dx==0&&((mouse.x>3&&mouse.x<(width()/gridSize-2))||(mouse.y>3&&mouse.y<(height()/gridSize-2)))){
            if(mouse.y<cat.y&&mouse.y>3&&mouse.y<height()/2/gridSize)dy=-3;
            if(mouse.y<cat.y&&mouse.y<=3){
                if(mouse.x>3&&mouse.x<width()/2/gridSize){
                    dx=-3;
                }else if(mouse.x<(width()/gridSize-2)&&mouse.x>=width()/2/gridSize){
                    dx=3;
                }
            }
            if(mouse.y>cat.y+catSize-1&&mouse.y>height()/2/gridSize&&mouse.y<(height()/gridSize-2))dy=3;
            if(mouse.y>cat.y+catSize-1&&mouse.y>=(height()/gridSize-2)){
                if(mouse.x>3&&mouse.x<width()/2/gridSize){
                    dx=-3;
                }else if(mouse.x<(width()/gridSize-2)&&mouse.x>=width()/2/gridSize){
                    dx=3;
                }
            }
        }

        if(dy==0&&mouse.x>3&&mouse.x<(width()/gridSize-2)&&mouse.y>3&&mouse.y<(height()/gridSize-2)){
            if(mouse.x<cat.x&&mouse.x>3)dx=-3;
            if(mouse.x<cat.x&&mouse.x<=3){
                if(mouse.y>3&&mouse.y<height()/2/gridSize){
                    dy=-3;
                }else if(mouse.y<(height()/gridSize-2)&&mouse.y>=height()/2/gridSize){
                    dy=3;
                }
            }
            if(mouse.x>cat.x+catSize-1)dx=3;
            if(mouse.x>cat.x+catSize-1&&mouse.x>=(width()/gridSize-2)){
                if(mouse.y>3&&mouse.y<height()/2/gridSize){
                    dy=-3;
                }else if(mouse.y<(height()/gridSize-2)&&mouse.y>=height()/2/gridSize){
                    dy=3;
                }
            }
        }

        if(dx!=0&&dy!=0&&mouse.x>3&&mouse.x<(width()/gridSize-2)&&mouse.y>3&&mouse.y<(height()/gridSize-2)){
            if(QRandomGenerator::global()->bounded(2)==0){
                dy=0;
            }else{
                dx=0;
            }
        }

        //被猫追到角落
        if(mouse.x<=3&&mouse.y<=3){
            if(dx==0){
                dx=10;
            }
            else if(dy==0){
                dy=10;
            }else{
                dx=10;
            }
        }
        if(mouse.x>=(width()/gridSize-2)&&mouse.y<=3){
            if(dx==0){
                dx=-10;
            }
            else if(dy==0){
                dy=10;
            }else{
                dx=-10;
            }
        }
        if(mouse.x>=(width()/gridSize-2)&&mouse.y>=(height()/gridSize-2)){
            if(dx==0){
                dx=-10;
            }
            else if(dy==0){
                dy=-10;
            }else{
                dy=-10;
            }
        }
        if(mouse.x<=3&&mouse.y>=(height()/gridSize)-2){
            if(dx==0){
                dx=10;
            }
            else if(dy==0){
                dy=-10;
            }else{
                dy=-10;
            }
        }

        int newX=mouse.x+dx;
        int newY=mouse.y+dy;

        if(newX>=0&&newX<=(width()/gridSize)){
            mouse.x=newX;
        }
        if(newY>=0&&newY<=(height()/gridSize)){
            mouse.y=newY;
        }
    }else{
        int direction=QRandomGenerator::global()->bounded(4);
        int newX=mouse.x;
        int newY=mouse.y;

        switch(direction){
        case 0:newX--;break;
        case 1:newX++;break;
        case 2:newY--;break;
        case 3:newY++;break;
        }

        if(newX>=0&&newX<=(width()/gridSize)){
            mouse.x=newX;
        }
        if(newY>=0&&newY<=(height()/gridSize)){
            mouse.y=newY;
        }
    }

    update();
}

bool GameWidget::isCatNearby(int mouseX,int mouseY){
    for(int x=mouseX-mousefeel;x<=mouseX+mousefeel;++x){
        for(int y=mouseY-mousefeel;y<=mouseY+mousefeel;++y){
            if(x>=cat.x&&x<cat.x+catSize&&y>=cat.y&&y<cat.y+catSize){
                return true;
            }
        }
    }
    return false;
}

void GameWidget::checkCollision(){
    if((mouse.x>=cat.x&&mouse.x<cat.x+catSize&&mouse.y>=cat.y&&mouse.y<cat.y+catSize)||(mouse.x+mouseSize-1>=cat.x
  &&mouse.x+mouseSize-1<cat.x+catSize&&mouse.y+mouseSize-1>=cat.y&&mouse.y+mouseSize-1<cat.y+catSize))
    {
        if(firstcatched){
        gameTimer->stop();
        mouseTimer->stop();
        countdownTimer->stop();
        emit gameOver(true);
        }else {
            firstcatched=true;
            mouseup();
        }
    }
}

void GameWidget::mouseup(){
  //  Q_UNUSED(event);

    mousefeel=10;
  if(mouse.x<height()/2/gridSize){
    mouse.x=cat.x+10;
  }else{
      mouse.x=cat.x-10;
  }
  if(mouse.y<width()/2/gridSize){
      mouse.y=cat.y+10;
  }else{
      mouse.y=cat.y-10;
  }
}
